<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="exporFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="归属" value="1"></el-option>
              <el-option label="sn码" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>

    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="110" align="center" prop="username" label="归属"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="100" align="center" prop="app_package" label="开发者模式">
          <template slot-scope="scope">{{scope.row.is_developer ? '开启' : '关闭'}}</template>
      </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="120" align="center" prop="value" label="SN码"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="140" align="center" prop="update_time" label="更新时间">
        <!-- <template slot-scope="scope">{{scope.row.update_time| newDate}}</template> -->
      </el-table-column>
      <!-- <el-table-column min-width="40" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" @click="getdeleteLabel(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <el-dialog title="添加" :visible.sync="exporFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="30%">
      <el-form :model="addForm" :rules="rules" ref="ruleForm">
        <el-form-item label="开发者模式" label-width="100px" prop="is_developer">
          <el-radio-group v-model="addForm.is_developer">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="SN" label-width="100px" prop="sn">
          <el-input v-model="addForm.sn" class="add_select" placeholder="请输入SN码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="exporFormVisible = false" size="mini" round>取 消</el-button>
        <el-button type="primary" size="mini" round @click="getaddDeveloperSn('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryDeveloperSn, addDeveloperSn } from "@/api/lock.js";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["归属", "开发者模式", "SN码", "更新时间"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "", //搜索条件
      //浏览器专题id
      subjectId: 0,
      exporFormVisible: false,
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), username: undefined, sn: undefined },
      addForm: { sn: "", is_developer: 0 },
      rules: { sn: [{ required: true, message: "请输入SN", trigger: "blur" }], is_developer: [{ required: true, message: "请选择", trigger: "blur" }] },
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal);//转为数组对象，好控制table
    this.getqueryDeveloperSn()
  },
  methods: {
    //添加
    getaddDeveloperSn(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.last_ip = sessionStorage.getItem("Ip")
          addForm.userId = localStorage.getItem("ID")
          console.log('参数',addForm)
          addDeveloperSn(addForm).then(res => {
            this.$message.success(res.msg)
            this.getqueryDeveloperSn()
            this.exporFormVisible = false
          }).catch(err => { })
        } else {
          return false;
        }
      });
    },
    getqueryDeveloperSn() {
      queryDeveloperSn(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
        console.log('数据', res.data)
      }).catch(err => {
        this.browserTable = []
        this.total = 0
      });
    },
    //搜索,1:sn码,2,归属
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.username = this.searchTable
          this.getqueryDeveloperSn();
          break;
        case '2':
          this.listQuery.sn = this.searchTable
          this.getqueryDeveloperSn();
          break;
      }
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryDeveloperSn();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryDeveloperSn();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), username: undefined, sn: undefined }
      this.getqueryDeveloperSn()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>